package com.android.lovingpet.adopt.chat;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.base.BaseActivity;
import com.android.lovingpet.base.BasePresenter;
import com.android.lovingpet.base.BaseView;
import com.android.lovingpet.bean.Account;
import com.android.lovingpet.bean.Team;
import com.android.lovingpet.util.Utility;

import java.util.ArrayList;
import java.util.List;

public class ChatActivity extends BaseActivity implements View.OnClickListener {

    private static final String MSG0 = "终于等到你啦！\n咨询养宠事宜请回复1\n咨询志愿者事宜请回复2\n其他帮助请回复3";
    private static final String MSG1 = "意愿领养人：您好！我们已将意愿领养表发送到您的邮箱，希望您能认真阅读和填写，填写后发送给我们，我们的工作人员将在3-5个工作日内给您答复，如符合条件工作人员将会主动与您联系，感谢您的配合！";
    private static final String MSG2 = "这里没有消息快滚吧！！！";
    private static final String MSG3 = "cls学姐24小时人工热线：13027498200";
    private List<ChatAdapter.Message> messages;
    private RecyclerView rvMessage;
    private ChatAdapter adapter;
    private EditText etText;
    private Button btSend;
    private Team you;
    private Account me;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        you = getIntent().getParcelableExtra("team");
        me = Utility.getAccount();

        rvMessage = findViewById(R.id.rv_msg);
        etText = findViewById(R.id.et_text);
        btSend = findViewById(R.id.bt_send);
        ((TextView) findViewById(R.id.tv_title)).setText(you.getName());
        messages = new ArrayList<>();
        adapter = new ChatAdapter(messages);
        rvMessage.setLayoutManager(new LinearLayoutManager(this));
        rvMessage.setAdapter(adapter);
        btSend.setOnClickListener(this);
        addMessage(new ChatAdapter.Message(null, you, MSG0));
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public BaseView createView() {
        return this;
    }

    @Override
    public void onClick(View v) {
        String text = etText.getText().toString().trim();
        etText.getText().clear();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
        }
        messages.add(new ChatAdapter.Message(me, null, text));
        if (text.equals("1")) {
            addMessage(new ChatAdapter.Message(null, you, MSG1));
        } else if (text.equals("2")) {
            addMessage(new ChatAdapter.Message(null, you, MSG2));
        } else if (text.equals("3")) {
            addMessage(new ChatAdapter.Message(null, you, MSG3));
        }
    }

    public void addMessage(ChatAdapter.Message message) {
        messages.add(message);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyItemInserted(messages.size());
                adapter.notifyItemRangeChanged(messages.size(),1);
                rvMessage.scrollToPosition(messages.size() - 1);
            }
        });
    }
}
